
<template>
  <p>
    <el-button @click="dialogUser = true" type="warning" class="el-icon-edit-outline" size="small"></el-button>
    <el-dialog title="修改用户" :modal="false" :visible.sync="dialogUser">
      <el-form v-if="form1" :model="form1">
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="form1.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" :label-width="formLabelWidth">
          <el-input v-model="form1.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" :label-width="formLabelWidth">
          <el-select v-model="form1.usertype" auto-complete="off">
            <el-option
              v-for="item in usertypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择地区" :label-width="formLabelWidth">
          <el-select v-model="form1.area" auto-complete="off">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户手机" :label-width="formLabelWidth">
          <el-input v-model="form1.phonenum" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" :label-width="formLabelWidth">
          <el-input v-model="form1.useremail" auto-complete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="用户标签" :label-width="formLabelWidth">
          <el-input v-model="form1.classification" auto-complete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="用户地址" :label-width="formLabelWidth">
          <el-cascader
            :options="address()"
            :props="{value:'label'}"
            v-model="selectedOptions"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUser= false">取 消</el-button>
        <el-button type="primary" @click="onSubmit1">确 定</el-button>
      </div>
    </el-dialog>
  </p>
</template>
<script>
import { asyncupdateUser} from "../../../plugins/api.js";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      formLabelWidth: "120px",
      dialogUser: false,
      form1: {},
    //  classification:"",
      selectedOptions: []
    };
  },
  watch: {
    dialogUser() {
      this.form1 = JSON.parse(JSON.stringify(this.row));
      if(this.form1.address) {
        this.selectedOptions[0] = this.form1.address.province
        this.selectedOptions[1] = this.form1.address.city
        this.selectedOptions[2] = this.form1.address.area
      }
    }
  },
  computed: {
    ...mapGetters([ "getuser"]),
  },
  methods: {
    address() {
      const address = window.address || [];
      return address;
    },
    async onSubmit1() {
      console.log(this.getuser.userid)
      try {
        await asyncupdateUser(this.form1);
        this.$store.dispatch("actionUser");
        this.$message({
          message: "恭喜你，修改成功",
          type: "success"
        });
        this.dialogUser = false;
      } catch (err) {
        this.$message.error("错了哦，修改失败:" + err);
      }
    },
    handleChange() {
      const data = {
        province: this.selectedOptions[0],
        city: this.selectedOptions[1],
        area: this.selectedOptions[2]
      };
      this.form1.address = data;
    }
  },

  props: ["options", "usertypes", "row"]
};
</script>

<style scoped>
p {
  display: inline-block;
  margin-right: 10px;
}
</style>
